//
//  KDCityController.m
//  PublicSentimentMonitorSystem
//
//  Created by 魏强 on 15/8/11.
//  Copyright (c) 2015年 魏强. All rights reserved.
//

#import "KDCityController.h"
#import "KDReginType.h"
#import "KDReginDataTool.h"
#import "KDTownController.h"
#import "KDReportController.h"
#import "KDSearchResultController.h"

@interface KDCityController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) NSArray *infoArr;
@property (nonatomic, strong) UITableView *tableView;

@end

@implementation KDCityController
#pragma mark - 懒加载区
- (NSArray *)infoArr{
    if (!_infoArr) {
        NSArray *arr = (self.fromWeibo)?[KDReginDataTool weiboReginTypeArrWithParentId:self.ReginType.ID]:[KDReginDataTool reginTypeArrWithParentId:self.ReginType.ID];
        NSMutableArray *mutableArr = [NSMutableArray arrayWithArray:arr];
        [mutableArr insertObject:self.ReginType atIndex:0];
        _infoArr = [NSArray arrayWithArray:mutableArr];
    }
    return _infoArr;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = self.address;
    self.view.backgroundColor = BackGroundColor;
    [self setUpTableView];
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [self.tableView setLayoutMargins:UIEdgeInsetsZero];
    }
}
- (void)setUpTableView{
    UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(10  , 10, self.view.width -20, self.view.height - 64 -10) style:UITableViewStylePlain];
    tableView.tableFooterView = [UIView new];
    tableView.separatorColor = BackGroundColor;
    self.tableView = tableView;
    //    tableView.editing = YES;
    tableView.delegate = self;
    tableView.dataSource = self;
    [self.view addSubview:tableView];
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        
        [cell setSeparatorInset:UIEdgeInsetsZero];
        
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return self.infoArr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *cellId =@"cityCell";
    
    UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:cellId];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
        
    }
    KDReginType *model = self.infoArr[indexPath.row];
    cell.textLabel.text = model.name;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    UIViewController *VC ;
    for (UIViewController *vc in self.navigationController.viewControllers) {//找出出显示地区的控制器
        if ([vc isKindOfClass:[KDBaseRegionController class]]) {
            if ([vc isKindOfClass:[KDSearchResultController class]]) {
                KDSearchResultController *searchCtrl = (KDSearchResultController *)vc;
                if (searchCtrl.here) {
                    VC = vc;break;
                }
            }else{
                VC = vc;break;
            }
            
        }
    }
    if (indexPath.row == 0) {//如果是第一行的，那就是全省的
        [[NSNotificationCenter defaultCenter] postNotificationName:@"CityAddressToReportControllerNoty" object:nil userInfo:@{@"address":self.address, @"regionModel":self.ReginType}];
        
        [self.navigationController popToViewController:VC
                                              animated:YES];
        return;
    }
    KDReginType *model =  self.infoArr[indexPath.row];
    //如果是四个直辖市的,就两级跳转，或者如果是search界面过来的就两级跳
    NSArray *arr = @[@(1), @(25), @(1169), @(3438)];
    if ([arr containsObject:@(_ReginType.ID)] || [VC isKindOfClass:[KDSearchResultController class]]) {
        self.address = [NSString stringWithFormat:@"%@  %@", self.address, model.name];
         [[NSNotificationCenter defaultCenter] postNotificationName:@"CityAddressToReportControllerNoty" object:nil userInfo:@{@"address":self.address, @"regionModel":model}];
        [self.navigationController popToViewController:VC
                                              animated:YES];
        return;
    }
    KDTownController *townCV = [[KDTownController alloc]init];
    townCV.ReginType = model;
    townCV.address = [NSString stringWithFormat:@"%@  %@", self.address, model.name];
    
    self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:townCV animated:YES];
    self.hidesBottomBarWhenPushed = NO;
}
@end
